package com.mao;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;

@SpringBootTest
public class MaoCode {
    public static void main(String[] args) {

        //构建一个代码生成器对象
        AutoGenerator mpg = new AutoGenerator();

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //E:\JavaWork\mybatis_plus\src
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("mao");
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setServiceName("%Service");  //去Service的I前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        //2、设置数据源
        DataSourceConfig dc = new DataSourceConfig();
        dc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dc.setUsername("root");
        dc.setPassword("123456");
        dc.setDriverName("com.mysql.cj.jdbc.Driver");
        dc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dc);

        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");
        pc.setParent("com.mao");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setController("controller");
        pc.setService("service");
        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig sc = new StrategyConfig();
        //设置要映射的表名
        sc.setInclude(
                "blog_tags","course","links","sys_settings","user_record","user_say");
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true);
        sc.setLogicDeleteFieldName("deleted");
        //自动填充
        TableFill gmtCrate = new TableFill("gmt_crate", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCrate);
        tableFills.add(gmtModified);
        sc.setTableFillList(tableFills);
        //乐观锁
        sc.setVersionFieldName("version");
        sc.setRestControllerStyle(true);
        //localhost:8080/hello_id_2
        sc.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(sc);

        mpg.execute();   //执行代码生成器
    }
}
